package com.hnucm.c201901020121.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.hnucm.c201901020121.Fragment.Home.HomeFragment;
import com.hnucm.c201901020121.Fragment.Lobby.LobbyFragment;
import com.hnucm.c201901020121.Fragment.Message.MessageFragment;
import com.hnucm.c201901020121.Fragment.My.MyFragment;
import com.hnucm.c201901020121.R;
import com.hnucm.c201901020121.Utils.Bmob.BUser;
import com.hnucm.c201901020121.Utils.Bmob.BmobTask;
import com.hnucm.c201901020121.Utils.OtherUtils.DatabaseHelper;

import org.xutils.x;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SQLQueryListener;
import cn.bmob.v3.listener.SaveListener;

import static com.hnucm.c201901020121.Activity.LoginAct.currentUser;

public class MainActivity extends AppCompatActivity {
    HomeFragment homeFragment = new HomeFragment();
    LobbyFragment lobbyFragment = new LobbyFragment();
    MessageFragment messageFragment = new MessageFragment();
    MyFragment myFragment=new MyFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        x.Ext.init(getApplication());
        DatabaseHelper helper=new DatabaseHelper( this );
        Bmob.initialize(this, "7a61c2b6351f923a4db4882d696a5767");
        ConstraintLayout constraintLayout = findViewById(R.id.constraintLayout2);
        ConstraintLayout constraintLayout1 = findViewById(R.id.constraintLayout);
        ConstraintLayout constraintLayout2 = findViewById(R.id.constraintLayout3);
        ConstraintLayout constraintLayout3=findViewById(R.id.constraintLayout4);
        constraintLayout.setSelected(true);
        constraintLayout1.setSelected(false);
        constraintLayout2.setSelected(false);
        constraintLayout3.setSelected(false);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment,homeFragment).commit();
        constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,homeFragment).commit();
                constraintLayout.setSelected(true);
                constraintLayout1.setSelected(false);
                constraintLayout2.setSelected(false);
                constraintLayout3.setSelected(false);
            }
        });
        constraintLayout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,lobbyFragment).commit();
                constraintLayout.setSelected(false);
                constraintLayout1.setSelected(true);
                constraintLayout2.setSelected(false);
                constraintLayout3.setSelected(false);
            }
        });
        constraintLayout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                constraintLayout.setSelected(false);
                constraintLayout1.setSelected(false);
                constraintLayout2.setSelected(true);
                constraintLayout3.setSelected(false);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,messageFragment).commit();
            }
        });
        constraintLayout3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                constraintLayout.setSelected(false);
                constraintLayout1.setSelected(false);
                constraintLayout2.setSelected(false);
                constraintLayout3.setSelected(true);
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,myFragment).commit();
            }
        });
        //getDate();
    }
    private void insertDate(){
        BmobTask bmobTask=new BmobTask();
        bmobTask.setAmount("30");
        bmobTask.setTitle("bmob测试");
        bmobTask.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e==null) {
                    Toast.makeText(MainActivity.this, "添加数据成功，返回objectId为：", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this, "添加数据失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void getBmobDate(){
        String sql="select * from BmobTask where amount='30'";
        BmobQuery<BmobTask> query=new BmobQuery<BmobTask>();
        query.setSQL(sql);
        query.doSQLQuery(new SQLQueryListener<BmobTask>() {
            @Override
            public void done(BmobQueryResult<BmobTask> bmobQueryResult, BmobException e) {
                if(e==null){
                    List<BmobTask> list=(List<BmobTask>) bmobQueryResult.getResults();
                    Log.i("Bmob",list.get(0).amount);
                }
                else {
                    Log.i("Bmob", "查找失败");
                }
            }
        });
    }
    private void getDate() {
        BmobQuery<BUser> bmobQuery = new BmobQuery<BUser>();
        bmobQuery.getObject("WvYL111K", new QueryListener<BUser>() {
            @Override
            public void done(BUser object, BmobException e) {
                if(e==null){
                    currentUser=object;
                }else{
                    Log.i("bmob",e.toString());
                    Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}